﻿/*********************
 * Fps监测
 * 魏继琼
 * 2011年10月7日15:45:27
 * ********************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VideoFlowStatistics.Class
{
    class FpsCount
    {
        // 帧数统计长度
        private  const int statLength = 15;
        // 目前帧数统计指数
        private static int statIndex = 0;
        // 准备帧数统计值
        private static int statReady = 0;
        // 帧数统计
        private static int[] statCount = new int[statLength];
        public static float fpsResult(int FramesReceived)
        {
            // 最后一秒的帧数
            statCount[statIndex] = FramesReceived;

            // 增量索引
            if (++statIndex >= statLength)
                statIndex = 0;
            if (statReady < statLength)
                statReady++;

            float fps = 0;

            // 计算平均值
            for (int i = 0; i < statReady; i++)
            {
                fps += statCount[i];
            }
            fps /= statReady;

            statCount[statIndex] = 0;

            return fps;
        }

        public static void fpsReset()
        {
            // 重置统计
            statIndex = 0;
            statReady = 0;
        }
    }
}
